﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using Wheeler.Web.Utility;

namespace Wheeler.Web
{
    // Note: For instructions on enabling IIS6 or IIS7 classic mode, 
    // visit http://go.microsoft.com/?LinkId=9394801

    public class MvcApplication : System.Web.HttpApplication
    {
        public static void RegisterGlobalFilters(GlobalFilterCollection filters)
        {
            filters.Add(new HandleErrorAttribute());
        }

        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");            
            //var nameFormat = @"[\-\w\d]+";
            //var actionFormat = @"\w+";
            //routes.MapRoute(
            //    AppConstants.RouteNames.Public.ITEM_DETAILS,
            //    "job/details/{id}/{jobKey}", 
            //    new { action = "Details", controller = "Job" }, 
            //    new { jobKey = nameFormat }, 
            //    new String[] {"Wheeler.Web.Controllers"} 
            //);
            //routes.MapRoute(
            //    AppConstants.RouteNames.Public.COMPANY_DETAILS,
            //    "{CompanyKey}",
            //    new { controller = "Company", action = "Details" },
            //    new { CompanyKey = String.Format("({0})", ApplicationCache.CompanyRoutes) },
            //    new String[] { "Wheeler.Web.Controllers" }
            //);
            //routes.MapRoute(
            //    AppConstants.RouteNames.Public.COMPANY_JOB_DETAILS,
            //    "{CompanyKey}/{id}/{JobKey}",
            //    //new { controller = "Company", action = "ProductDetails" },
            //    new { controller = "Job", action = "Details" },
            //    new { CompanyKey = String.Format("({0})", ApplicationCache.CompanyRoutes), JobKey = nameFormat },
            //    new String[] { "Wheeler.Web.Controllers" }
            //);
            //routes.MapRoute(
            //    AppConstants.RouteNames.Public.CONTENT_DETAILS,
            //    "{ContentUrl}",
            //    new { controller = "Content", action = "Details" },
            //    new { ContentUrl = String.Format("({0})", ApplicationCache.ContentRoutes) },
            //    new String[] { "Wheeler.Web.Controllers" }
            //);
            routes.MapRoute(
                AppConstants.RouteNames.Public.DEFAULT, // Default Public Route name
                "{controller}/{action}/{id}", // URL with parameters
                new { controller = "Home", action = "Index", id = UrlParameter.Optional },
                new String[] { "Wheeler.Web.Controllers" } 
            );
        }

        /// <summary>
        /// ASP .NET Application Start Event
        /// </summary>
        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();

            RegisterGlobalFilters(GlobalFilters.Filters);
            RefreshRoutes(false);
            //RegisterRoutes(RouteTable.Routes);
            ///To Debug Routings enable following line and rebuild
            //RouteDebug.RouteDebugger.RewriteRoutesForTesting(RouteTable.Routes);
            ///Configure Auto Mapper
            Wheeler.Web.Utility.BootStrapper.ConfigureTypeMappings();

            Wheeler.Web.Utility.BootStrapper.SetUserRoles();
            Wheeler.Web.Utility.BootStrapper.PrepareDomainUtility();
            Wheeler.Common.Mail.MailManager.SmtpHost = Wheeler.Web.Utility.ConfigReader.SmtpHost;
            Wheeler.Common.Mail.MailManager.SmtpPort = Wheeler.Web.Utility.ConfigReader.SmtpPort;
            //Wheeler.Web.Utility.LuceneEngine.LuceneIndexDirectory = Wheeler.Web.Utility.ConfigReader.LuceneIndexDirectory;
            //Wheeler.Web.Utility.LuceneEngine.BuildSearchIndex();
        }

        protected void Application_Error(Object sender, EventArgs e)
        {
            if(ConfigReader.SendErrorEmail)
                BootStrapper.SendErrorEmail(Server.GetLastError());
        }

        /// <summary>
        /// Refreshes routes for Company and Content
        /// </summary>
        /// <param name="forceRebuild"></param>
        public static void RefreshRoutes(bool forceRebuild)
        {
            if (RouteTable.Routes.Count > 0)
                RouteTable.Routes.Clear();

            ///Prepare the cache of URL Routes
            //ApplicationCache.RefreshContentRouteCache(forceRebuild);
            //ApplicationCache.RefreshCompanyRouteCache(forceRebuild);

            AreaRegistration.RegisterAllAreas();
            RegisterRoutes(RouteTable.Routes);
        }
    }
}